package com.mhkj.check.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.mhkj.check.entity.FineDetail;
import com.mhkj.check.repository.FineDetailRepository;
import com.mhkj.check.service.FineDetailService;
import com.mics.core.common.componet.Pagination;
import com.mics.core.system.base.service.impl.BaseServiceImpl;

/**
 * 罚款明细
 * @author mics
 * @date 2018年8月27日
 * @version 1.0
 */
@Service
public class FineDetailServiceImpl extends BaseServiceImpl<FineDetail> implements FineDetailService{
	@Resource
	private FineDetailRepository fineDetailRepository;
	
	@Resource
	public void setFineDetailRepository(FineDetailRepository fineDetailRepository) {
		super.setBaseRepository(fineDetailRepository);
	}


	/*public Pagination<FineDetail> fruitArchivesDetailedListByPage(FineDetail fineDetail, String dateTemp, int i, int pageIndex, int pageSize) {

		return null;
	}
*/


	@Override
	public Pagination<FineDetail> fruitArchivesDetailedListByPage(FineDetail fineDetail, String dateTemp, int i, int pageIndex, int pageSize) {
		Specification<FineDetail> spec = new Specification<FineDetail> () {
			private static final long serialVersionUID = -786003485941679959L;
			@Override
			public Predicate toPredicate(Root<FineDetail> root, CriteriaQuery<?> query,
										 CriteriaBuilder criteriaBuilder) {
				List<Predicate> lstPredicates = new ArrayList<Predicate>();

				if(fineDetail.getPlanNode() != null){
					Join<Object, Object> planType = root.join("planNode",JoinType.INNER);
					Predicate p1=criteriaBuilder.equal(planType.get("id").as(Long.class), fineDetail.getPlanNode().getId());
					lstPredicates.add(p1);
				}
				Predicate[] arrayPredicates = new Predicate[lstPredicates.size() ];
				return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
			}
		};
		Page<FineDetail> pageT = fineDetailRepository.findAll(spec, PageRequest.of(pageIndex, pageSize, Sort.Direction.DESC, "createDate"));
		Pagination<FineDetail> pagination = new Pagination<FineDetail>();
		pagination.setTotal(fineDetailRepository.count(spec));
		pagination.setData(pageT.getContent());
		return pagination;

	}
}
